package com.eeepay.eeepay_v2.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.eeepay_v2.util.GrpcUtils;
import com.eeepay.v2_library.ui.ABBaseActivity;
import com.eeepay.v2_library.util.ImageLoaderUtils;
import com.eeepay.v2_library.view.HorizontalItemView;

import java.util.ArrayList;

import cn.eeepay.api.grpc.nano.QRcodeApiServiceGrpc;
import cn.eeepay.api.grpc.nano.QRcodeProto;
import cn.eeepay.api.grpc.nano.ReqBaseProto;
import io.grpc.ManagedChannel;

/**
 * Created by zw on 2016/4/13 0013.
 * 二维码界面
 */
public class QRCodeActivity extends ABBaseActivity implements View.OnClickListener {

    private TextView companyName,  publicName;//公司名称、  公众号名
    private ImageView publicQRCode;// 公众号二维码
    private HorizontalItemView chooseProduct;//选择产品
    private TextView productName;//产品名称
    private ImageView productQRCode;//产品二维码

    private QRcodeProto.QRcode[] products;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_qr_code;
    }

    @Override
    protected void initWidget() {
        companyName = getViewById(R.id.tv_company_name);
        publicName = getViewById(R.id.tv_public_name);
        publicQRCode = getViewById(R.id.iv_public_qr_code);
        chooseProduct = getViewById(R.id.hv_choose_product);
        productName = getViewById(R.id.tv_product_name);
        productQRCode = getViewById(R.id.iv_product_qr_code);

        sendHttpRequest(0);
    }

    @Override
    protected void eventOnClick() {
        chooseProduct.setOnClickListener(this);
    }

    @Override
    protected void sendHttpRequest(int taskId) {
        showProgressDialog();
        GrpcUtils.getInstance().doGrpcTask(Constant.URL.URL, Constant.URL.PORT,
                taskId, new GrpcUtils.AsyncTaskListener() {
            @Override
            public Object doInBack(ManagedChannel channel, int taskId) {
                QRcodeApiServiceGrpc.QRcodeApiServiceBlockingStub stub = QRcodeApiServiceGrpc.newBlockingStub(channel);
                ReqBaseProto.ReqOneStr request = new ReqBaseProto.ReqOneStr();
                request.param = "10025";// 代理商编号 10025、 8586
                return stub.queryAllQrCodeList(request);
            }

            @Override
            public void updateUI(Object result, int taskId) {
                dismissProgressDialog();
                if(result == null){
                    showToast("查询失败");
                    return;
                }
                QRcodeProto.QRcodeList response = (QRcodeProto.QRcodeList) result;
                if(!response.resStatus.status){
                    showToast(response.resStatus.msg);
                    return;
                }
                companyName.setText(response.company);
                publicName.setText(response.public_.name);
                ImageLoaderUtils.loadBitmap(response.public_.url, publicQRCode);
                QRCodeActivity.this.products = response.products;
                if(products != null && products.length > 0){
                    productName.setText(products[0].name);
                    ImageLoaderUtils.loadBitmap(products[0].url, productQRCode, R.mipmap.appicon, R.drawable.icon_delete);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.hv_choose_product:
                if(products != null && products.length > 0){
                    Bundle bundle = new Bundle();
                    ArrayList<String> list = new ArrayList<>();
                    for(int i = 0; i < products.length; i++){
                        list.add(products[i].name);
                    }
                    bundle.putString(Constant.INTENT_FLAG, Constant.PRODUCT_LIST);
                    bundle.putStringArrayList(Constant.PRODUCT_LIST, list);
                    goActivityForResult(ChooseActivity.class, bundle, 0);
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode != RESULT_OK){
            return;
        }
        if(data != null){
            String name = data.getStringExtra(Constant.PRODUCT_LIST);
            if(!TextUtils.isEmpty(name)){
                productName.setText(name);
                if(products != null && products.length > 0){
                    for(int i = 0; i < products.length; i++){
                        if(products[i].name.equals(name)){
                            ImageLoaderUtils.loadBitmap(products[i].url, productQRCode, R.mipmap.appicon, R.drawable.icon_delete);
                        }
                    }
                }
            }
        }
    }
}
